/**
 * 
 */
package com.ram.app.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * druid 数据源配置
 * 
 * @author fudongjing
 * @since 2017年12月14日
 */
@Configuration
public class DruidConfig {

	private static final Logger log = LoggerFactory.getLogger(com.ram.app.config.DruidConfig.class);

	@Value("${spring.datasource.druid.driver-class-name}")
	private String driverClassName;
	@Value("${spring.datasource.druid.url}")
	String url;
	@Value("${spring.datasource.druid.username}")
	String username;
	@Value("${spring.datasource.druid.password}")
	String password;
	@Value("${spring.datasource.druid.initial-size}")
	int initialSize;
	@Value("${spring.datasource.druid.min-idle}")
	int minIdle;
	@Value("${spring.datasource.druid.max-active}")
	int maxActive;
	@Value("${spring.datasource.druid.max-wait}")
	long maxWait;
	@Value("${spring.datasource.druid.time-between-eviction-runs-millis}")
	long timeBetweenEvictionRunsMillis;
	@Value("${spring.datasource.druid.min-evictable-idle-time-millis}")
	long minEvictableIdleTimeMillis;
	@Value("${spring.datasource.druid.validation-query}")
	String validationQuery;
	@Value("${spring.datasource.druid.test-while-idle}")
	boolean testWhileIdle;
	@Value("${spring.datasource.druid.test-on-borrow}")
	boolean testOnBorrow;
	@Value("${spring.datasource.druid.test-on-return}")
	boolean testOnReturn;
	@Value("${spring.datasource.druid.pool-prepared-statements}")
	boolean poolPreparedStatements;
	@Value("${spring.datasource.druid.max-pool-prepared-statement-per-connection-size}")
	int maxPoolPreparedStatementPerConnectionSize;
	@Value("${spring.datasource.druid.filters}")
	String filters;
	@Value("${spring.datasource.druid.connection-properties}")
	String connectionProperties;
	
	/**
	 *初始化数据库连接池
	 */
	@Bean
	public DataSource druidDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setDriverClassName(driverClassName);
		druidDataSource.setUrl(url);
		druidDataSource.setUsername(username);
		druidDataSource.setPassword(password);
		druidDataSource.setInitialSize(initialSize);
		druidDataSource.setMinIdle(minIdle);
		druidDataSource.setMaxActive(maxActive);
		druidDataSource.setMaxWait(maxWait);
		druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		druidDataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
		druidDataSource.setValidationQuery(validationQuery);
		druidDataSource.setTestWhileIdle(testWhileIdle);
		druidDataSource.setTestOnBorrow(testOnBorrow);
		druidDataSource.setTestOnReturn(testOnReturn);
		druidDataSource.setPoolPreparedStatements(poolPreparedStatements);
		druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
		druidDataSource.setConnectionProperties(connectionProperties);
		try {
			druidDataSource.setFilters(filters);
			druidDataSource.init();
		} catch (SQLException e) {
			// e.printStackTrace();
			log.error(e.getMessage(), e);
		}

		return druidDataSource;
	}

	@Bean
	@ConditionalOnMissingBean
	public ServletRegistrationBean druidServlet() {
		// 访问 localhost://端口/druid 能够查看到druid的监控数据
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
				"/druid/*");
		// 添加初始化参数：initParams
		// 白名单：
		 servletRegistrationBean.addInitParameter("allow","*");
		// IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to
		// view this page.
		// servletRegistrationBean.addInitParameter("deny","192.168.1.73");
		// 登录查看信息的账号密码.
		servletRegistrationBean.addInitParameter("loginUsername", "admin");
		servletRegistrationBean.addInitParameter("loginPassword", "admin");
		// 是否能够重置数据.
		servletRegistrationBean.addInitParameter("resetEnable", "true");
		return servletRegistrationBean;

	}

	@Bean
	@ConditionalOnMissingBean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		return filterRegistrationBean;
	}

	/*
	 * 分页插件，自动识别数据库类型
	 *
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}


}
